package com.a2.appa3.ui.activity;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.ResIdear;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.shihao.library.XStatusBarHelper;

public class FeedbackActivity extends BaseActivitySimple {

    @BindView(R.id.ivback)
    ImageView ivback;
    @BindView(R.id.hinded)
    View hinded;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.headerButton)
    ImageView headerButton;
    @BindView(R.id.headerButtonRoot)
    LinearLayout headerButtonRoot;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.address_details_et_name)
    EditText addressDetailsEtName;
    @BindView(R.id.address_details_btn_submit)
    Button addressDetailsBtnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            //解决Android5.0以上，状态栏设置颜色后变灰的问题
            XStatusBarHelper.tintStatusBar(this, getResources().getColor(R.color.settingbg));
        }
        headerTitle.setText("建议反馈");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addressDetailsBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(addressDetailsEtName.getText().toString().trim())) {
                    putidear(addressDetailsEtName.getText().toString().trim());
                } else {
                    Toast.makeText(FeedbackActivity.this, "当前提交的内容不得为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        addressDetailsEtName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    hinded.setVisibility(View.VISIBLE);
                } else {
                    hinded.setVisibility(View.GONE);
                }
            }
        });
    }

    private void putidear(String trim) {
        //反馈投诉1 充值问题 2提款问题 3其他问题 4提交问题 5我要申诉
        HttpJsonUtils.getHttpApiInstance()
                .putidear(HttpAdress.getuseAdress() + "api/mobile/feedback", "4", trim, Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResIdear>() {

                    @Override
                    protected void onCustomNext(ResIdear resLogin) {
                        if (resLogin.code.equals("0") && resLogin.status) {
                            Toast.makeText(FeedbackActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                            addressDetailsEtName.setText("");
                        } else {
                            Toast.makeText(FeedbackActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_feedback;
    }
}
